package com.xrh.xdemo.service.service;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.SysDictDTO;
import com.xrh.xdemo.api.enums.ConfigTypeEnum;
import com.xrh.xdemo.api.request.addRequest.SysDictSaveRequest;
import com.xrh.xdemo.api.request.queryRequest.SysDictQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.SysDictUpdateRequest;
import com.xrh.xdemo.service.entity.SysDictEntity;
import com.xrh.xdemo.service.mapper.SysDictMapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

@Service
public class SysDictService {

	@Resource
	SysDictMapper sysDictMapper;
	
	/**
	 * @function 根据类型枚举获取entity(无业务key的情况)
	 * @param configTypeEnum
	 * @author 肖荣辉
	 * @date 2023年10月12日
	*/
	public SysDictEntity findEntityByTypeEnum(ConfigTypeEnum configTypeEnum) {
		return findEntityByTypeEnumAndBusinessKey(configTypeEnum, null);
	}
	
	/**
	 * @function 根据类型枚举和业务key获取entity
	 * @param configType
	 * @author 肖荣辉
	 * @date 2023年10月12日
	*/
	public SysDictEntity findEntityByTypeAndBusinessKey(String configType ,  Object businessKey) {
		return findEntityByTypeEnumAndBusinessKey(ConfigTypeEnum.valueOf(configType), businessKey);
	}
	
	/**
	 * @function 根据类型枚举和业务key获取entity
	 * @param configTypeEnum
	 * @author 肖荣辉
	 * @date 2023年10月12日
	 */
	public SysDictEntity findEntityByTypeEnumAndBusinessKey(ConfigTypeEnum configTypeEnum ,  Object businessKey) {
		
		//默认key为NO_KEY
		String key = businessKey != null  ? businessKey.toString() : null;
		
		LambdaQueryWrapper<SysDictEntity> queryWrapper = new LambdaQueryWrapper<SysDictEntity>();
		
		queryWrapper.eq(SysDictEntity::getType , configTypeEnum.name());
		
		if(key != null)
			queryWrapper.eq(SysDictEntity::getBusinessKey , key);
		
		queryWrapper.last("limit 1");
		
		return sysDictMapper.selectOne(queryWrapper);
	}

	
	/**
	 * @function 根据枚举类型和业务key获取配置信息
	 * @param configTypeEnum
	 * @param clazz
	 * @param businessKey
	 * @author 肖荣辉
	 * @date 2023年10月12日
	*/
	public <T> T findConfigByTypeEnum(ConfigTypeEnum configTypeEnum , Class<T> clazz , Object businessKey) {

		SysDictEntity entity = findEntityByTypeEnumAndBusinessKey(configTypeEnum , businessKey);
		
		if(entity == null)
			return null;
		
		String configInfo = entity.getConfigInfo();
		
		if(StrUtil.isBlank(configInfo))
			return null;
		
		return JSONUtil.toBean(configInfo, clazz);
		
	}
	
	/**
	 * @function 根据枚举类型和业务key获取配置信息
	 * @param configTypeEnum
	 * @param clazz
	 * @param businessKey
	 * @author 肖荣辉
	 * @date 2023年10月12日
	*/
	public String findConfigByTypeEnum(ConfigTypeEnum configTypeEnum ,  Object businessKey) {

		SysDictEntity entity = findEntityByTypeEnumAndBusinessKey(configTypeEnum , businessKey);
		
		if(entity == null)
			return null;
		
		String configInfo = entity.getConfigInfo();
		
		return configInfo;
		
	}
	
	/**
	 * @function 根据枚举类型和业务key获取配置信息
	 * @param configType
	 * @param clazz
	 * @param businessKey
	 * @author 肖荣辉
	 * @date 2023年10月12日
	 */
	public String findConfigByType(String configType ,  Object businessKey) {
		return findConfigByTypeEnum(ConfigTypeEnum.valueOf(configType) , businessKey);
	}
	
	/**
	 * @function 新增SysDict
	 * @param sysDictAddRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public void saveSysDict(SysDictSaveRequest saveRequest) {
		
		ConfigTypeEnum typeEnum = ConfigTypeEnum.valueOf(saveRequest.getType());
		
		SysDictEntity sysDictEntity = findEntityByTypeEnumAndBusinessKey(typeEnum, saveRequest.getBusinessKey());
		
		//新增
		if(sysDictEntity == null) {
			
			sysDictEntity = new SysDictEntity();
			sysDictEntity.setConfigInfo(saveRequest.getConfigInfo());
			sysDictEntity.setBusinessKey(saveRequest.getBusinessKey());
			sysDictEntity.setType(typeEnum.name());
			sysDictEntity.setTypeCnName(typeEnum.getShowName());
			
			sysDictMapper.insert(sysDictEntity);
			
			return;
		}
		
		//更新
		sysDictEntity.setConfigInfo(saveRequest.getConfigInfo());
		sysDictMapper.updateById(sysDictEntity);
		
	}

	/**
	 * @function 删除SysDict
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public void deleteSysDictById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		SysDictEntity sysDictEntity = sysDictMapper.selectById(id);
		
		if(sysDictEntity == null)
			throw new BusinessException("id不合法");
		
		UpdateWrapper<SysDictEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", id);
		
		//因为注解了@TableLogic , 自动会把删除标志位设置为1 
		sysDictMapper.delete(updateWrapper);
		
	}

	/**
	 * @function 修改SysDict
	 * @param sysDictUpdateRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public void updateSysDict(SysDictUpdateRequest sysDictUpdateRequest) {
		
		if(sysDictUpdateRequest.getId() == null)
			throw new BusinessException("id不能为空");
		
		SysDictEntity sysDictEntity = sysDictMapper.selectById(sysDictUpdateRequest.getId());
		
		if(sysDictEntity == null)
			throw new BusinessException("id不合法");
		
		BeanUtil.copyProperties(sysDictUpdateRequest , sysDictEntity , CopyOptions.create().ignoreNullValue());
		
		sysDictMapper.updateById(sysDictEntity);
		
	}

	/**
	 * @function 根据ID查询SysDict Entity
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public SysDictEntity findSysDictEntityById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		SysDictEntity sysDictEntity = sysDictMapper.selectById(id);
		
		return sysDictEntity;
	}
	
	/**
	 * @function 根据ID查询SysDict DTO
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public SysDictDTO findSysDictById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
	
		SysDictEntity sysDictEntity = sysDictMapper.selectById(id);
		
		SysDictDTO  sysDictDTO = BeanUtil.toBean(sysDictEntity , SysDictDTO.class);
		
		return sysDictDTO;
	}
	
	/**
	 * @function 根据queryRequest查询SysDict列表
	 * @param queryRequest
	 * @author 肖荣辉
	 * @date 2023-12-08 15:17:26
	*/
	public CommonResult<CommonPage<SysDictDTO>> findSysDictList(SysDictQueryRequest queryRequest) {
		
		Page<SysDictEntity> page = queryRequest.createPage();

		QueryWrapper<SysDictEntity> queryWrapper = queryRequest.createQuery();
		
		sysDictMapper.selectPage(page, queryWrapper);
		
		List<SysDictDTO> dtoList = BeanUtil.copyToList(page.getRecords() , SysDictDTO.class);
		
		return CommonResult.success(CommonPage.create(queryRequest.getPageNum() , queryRequest.getPageSize() , dtoList, page.getTotal()));
	}
	
}
